//
//  JPAddressListController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/18.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "JPAddressListController.h"
#import "JPAddressCell.h"
#import "JPModifyAddrController.h"
NSString *addressCellID = @"addressCellID";
@interface JPAddressListController ()<JPAddressDelegate>
@property (weak, nonatomic) IBOutlet UIButton *addAddressButton;
@property(nonatomic ,strong)NSArray<DHAddressModel *> *addressList;
@property(nonatomic ,strong)DHAddressModel *selectedModel;
@end

@implementation JPAddressListController

- (void)setAddressList:(NSArray<DHAddressModel *> *)addressList
{
    _addressList = addressList;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadAddressList];
    self.tableView.rowHeight = 120;
    //self.tableView.tableFooterView = [UIView new];
    self.addAddressButton.layer.masksToBounds = true;
    self.addAddressButton.layer.cornerRadius = 6;
}

- (void)loadAddressList
{
    [DHNetworkManager.shareManager requestAddressListWithCompletion:^(BOOL isSuccess, NSArray *addressList, NSString *errorInfo)
    {
        if (errorInfo) {
            [SVProgressHUD showErrorWithStatus:errorInfo];
            return ;
        }
        
        self.addressList = addressList;
    }];
}
#pragma mark: - 添加地址的按钮监听
- (IBAction)addAdrButtonClick:(UIButton *)sender
{
    JPModifyAddrController *vc = [JPModifyAddrController modifyVCWithModel:nil];
    vc.editOrAddAddressBlock =
    ^{
        self.addressList = nil;
        [self loadAddressList];
    };
    [self.navigationController pushViewController:vc animated:true];
}
#pragma mark: - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addressList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JPAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:addressCellID forIndexPath:indexPath];
    
    cell.model = self.addressList[indexPath.row];
    cell.delegate = self;
    return cell;
}

#pragma mark: - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSAssert(self.selectedAdrBlock != nil, @"self.selectedAdrBlock 不能为nil");
    
    self.selectedAdrBlock(self.addressList[indexPath.row]);
    [self.navigationController popViewControllerAnimated:true];
}

#pragma mark: - 编辑 / 删除 按钮的代理
- (void)addrCell:(JPAddressCell *)cell didModifyButton:(DHAddressModel *)model
{
    JPModifyAddrController *vc = [JPModifyAddrController modifyVCWithModel:model];
    vc.editOrAddAddressBlock =
    ^{
        self.addressList = nil;
        [self loadAddressList];
    };
    [self.navigationController pushViewController:vc animated:true];
}
- (void)addrCellDeleteButtonClick:(JPAddressCell *)cell deleteButtonClick:(DHAddressModel *)model
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = model.area_id;
    //param[@"key"] = [DHNetworkManager.shareManager userAccount].key;
    [DHNetworkManager.shareManager postEditAddressWithUrl:kDeleAdressUrl param:param
                                               completion:^(BOOL isSuccess, NSString *statusInfo)
     {
         if (!isSuccess)
         {
             [SVProgressHUD showErrorWithStatus:statusInfo];
             return ;
         }
         [SVProgressHUD showSuccessWithStatus:statusInfo];
         self.addressList = nil;
         [self loadAddressList];
     }];
}
@end
